﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using NUnit.Framework;
using PropertyManagement.Core;
using PropertyManagement.Core.DataAccess;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Test {
    [TestFixture]
    class DataAccessTest {
        [Test]
        public void TestAddSystemUser() {
            var entity = new SystemUser() {
                UserName = "bbb",
                Password = "123",
                CreationDate = DateTime.Today,
                LastUpdate = DateTime.Today,
                CreationUserID = 1,
                LastActionUserID = 1,
                RightLevel = RightLevel.CommonUser,
                IsValid = true,
                PasswordAnswer1 = "",
                PasswordAnswer2 = "",
                PasswordAnswer3 = "",
                PasswordQuestion1 = "",
                PasswordQuestion2 = "",
                PasswordQuestion3 = ""
            };
            var sql = @"insert into dbo.SystemUser values (@username,@password,@rightlevel,@isvalid,@passwordquestion1,@passwordquestion2,@passwordquestion3,@passwordanswer1,@passwordanswer2,@passwordanswer3,@creationdate,@lastupdate,@creationuserid,@lastactionuserid);
            select SCOPE_IDENTITY ();
            
";
            var outputP = new SqlParameter();
            outputP.ParameterName = "nid";
            outputP.Direction = ParameterDirection.Output;
            outputP.SqlDbType = SqlDbType.Int;

            var paramList = new SqlParameter[] {
							new SqlParameter("creationdate",entity.CreationDate),
							new SqlParameter("creationuserid",entity.CreationUserID),
							new SqlParameter("isvalid",entity.IsValid),
							new SqlParameter("lastactionuserid",entity.LastActionUserID),
							new SqlParameter("lastupdate",entity.LastUpdate),
							new SqlParameter("password",entity.Password),
							new SqlParameter("passwordanswer1",entity.PasswordAnswer1),
							new SqlParameter("passwordanswer2",entity.PasswordAnswer2),
							new SqlParameter("passwordanswer3",entity.PasswordAnswer3),
							new SqlParameter("passwordquestion1",entity.PasswordQuestion1),
							new SqlParameter("passwordquestion2",entity.PasswordQuestion2),
							new SqlParameter("passwordquestion3",entity.PasswordQuestion3),
							new SqlParameter("rightlevel",entity.RightLevel),
							new SqlParameter("username",entity.UserName)
                        };
            //SqlServerHelper.ExecuteNonQuery(SqlServerHelper.ConnectionString, CommandType.Text, sql, paramList); 
            using (
                var ireader = SqlServerHelper.ExecuteReader(SqlServerHelper.ConnectionString, CommandType.Text, sql,
                                                            paramList)) {
                if (ireader.Read()) {
                    entity.SystemUserID = int.Parse(ireader[0].ToString());
                }
            }
            Console.WriteLine(entity.SystemUserID);
        }

        [Test]
        public void TestAddSystemUser2() {
            var entity = new SystemUser( ) {
                UserName = "bbb",
                Password = "123",
                CreationDate = DateTime.Today,
                LastUpdate = DateTime.Today,
                CreationUserID = 1,
                LastActionUserID = 1,
                RightLevel = RightLevel.CommonUser,
                IsValid = true,
                PasswordAnswer1 = "",
                PasswordAnswer2 = "",
                PasswordAnswer3 = "",
                PasswordQuestion1 = "",
                PasswordQuestion2 = "",
                PasswordQuestion3 = ""
            };
            SystemUserDataAccess.Add(entity);
            Assert.AreNotEqual(entity.SystemUserID,0);
            SystemUserDataAccess.Remove(entity);
        }
    }
}
